import requests
import re


def request_maoyan_web(headers,page):
	# Get URL
	url = "http://maoyan.com/board/4?offset=" + str(page)
	res = requests.get(url,headers = headers)
	html = res.content.decode('utf-8')
	pat = '<dd>.*?<i class=.*?>(.*?)</i>.*?<img data-src="(.*?)".*?<p class="name"><a.*?>(.*?)</a></p>.*?<p class="star">(.*?)</p>.*?<p class="releasetime">上映时间：(.*?)</p>.*?<i class="integer">(.*?)</i>.*?<i class="fraction">(.*?)</i>.*?</dd>'
	items = re.findall(pat,html,re.S)
	# Print information
	for item in items:
		print('序号:' + str(item[0]).replace('\n','').strip() +'\n' +
		"电影名称:" + item[2].replace('\n','').strip() + '\n'
		"主演:" + str(item[3]).replace('\n','').strip()  + '\n' + 
		"时间:" + str(item[4]).replace('\n','').strip() + '\n' +
		"评分:" + str(item[5]).replace('\n','').strip() + str(item[6]).replace('\n','').strip() + '\n' +
		"图片连接:" + item[1].replace('\n','').strip() + '\n') 

if __name__ == '__main__':
	# Get headers
	headers = {
		'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
		'Accept-Language': 'zh-CN,zh;q=0.9',
		'Connection': 'keep-alive',
		'Cookie': 'uuid=1A6E888B4A4B29B16FBA1299108DBE9C7B0FF05888F4017B105DDE4484680436; _csrf=d35429a4c8a1151b56856c5673b12432a1f72cca00c09e73f0551695d5d1b0f7',
		'Host': 'maoyan.com',
		'Referer': 'http://maoyan.com/board/4?offset=0',
		'Upgrade-Insecure-Requests': '1',
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36',
	}
	print("Getting the Maoyan information, please wait minutes ....")
	for i in range(0,100,10):
		request_maoyan_web(headers,i)
